import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { AuthService } from 'services/auto.service';

@Injectable()
export class DbService {
  constructor(
    @InjectModel('user') private readonly LoginModel,
    public authService:AuthService,
  ) { }

  async login(params) {
    const {userName,passWord} = params;
    const uesrInfo = await this.LoginModel.find({userName}).exec();
    if(uesrInfo.length){
       if(uesrInfo[0] && uesrInfo[0]['passWord']===passWord){
         const token = await this.authService.createToken(userName);
          return {
            userName,
            msg:'登陆成功',
            success:true,
            token,
          }
       }else{
        return {
          msg:'密码错误',
          success:false
        }
       }
    }else{
      return {
        msg:'账号不存在',
        success:false
      }
    }
  }
   find(userName){
    return this.LoginModel.find({userName}).exec();
  }
}